let havingPoint=false;
window.addEventListener("load",()=>{
	const doc=this.document.getElementsByClassName("showNum")[0];
	
	doc.innerHTML=0;
	let buttons=document.querySelector(".buttons");
	buttons.addEventListener('click',(e)=>{
		let buttonValue=e.target.value;
		let showValue=doc.innerHTML;
		if(showValue.length>6&&showValue.length<9){
			doc.setAttribute("style",'font-size:40px')
		}else if(showValue.length>=9){
			doc.setAttribute("style",'font-size:30px')
		}
		else doc.setAttribute("style",'font-size:50px');
		if(!isNaN(buttonValue)){
			if(showValue!='0')
			doc.innerHTML=showValue+buttonValue;
			else doc.innerHTML=buttonValue;
		}else{
			
			switch(buttonValue){
				case 'AC':{
					doc.innerHTML=0;
					break;
				}
				case '+/-':{
					if(showValue[0]=='-') doc.innerHTML=showValue.substring(1,showValue.length);
					else doc.innerHTML='-'+showValue;
					break;
				}
				case '+':{
					if(addOperation(doc.innerHTML)){
						doc.innerHTML=showValue+'+';
					}
					break;
				}
				case '-':{
					if(addOperation(doc.innerHTML)){
						doc.innerHTML=showValue+'-';
					}
					break;
				}
				case '*':{
					if(addOperation(doc.innerHTML)){
						doc.innerHTML=showValue+'*';
					}
					break;
				}
				case '/':{
					if(addOperation(doc.innerHTML)){
						doc.innerHTML=showValue+'/';
					}
					break;
				}
				case '.':{
					if(addPoint(doc.innerHTML)) {
						doc.innerHTML=showValue+'.';
					}
					break;
				}
				case '=':{
					doc.innerHTML=getclc(doc.innerHTML);
					//
					break;
				}
				case '<-':{
					if(doc.innerHTML!=='0'){
						doc.innerHTML=doc.innerHTML.substring(0,doc.innerHTML.length-1);
						if(doc.innerHTML.length===0) doc.innerHTML='0';
					}
				}
				case 'm':{
					if(!isNaN(showValue[showValue.length-1])) doc.innerHTML=showValue+'%';
				}
			}
			
		}
	})
	
	
})
function addPoint(showContent){
	for(let x=showContent.length-1;x>=0;x--){
		if(showContent[x]==='.') return false;
		if(isNaN(showContent[x])) break;
	}
	return true;
}
function addOperation(showContent){
	if(isNaN(showContent[showContent.length-1])) return false;
	return true;
	
}
function init(showContent){
	// console.log(showContent)
	let arr=new Array();
	let c="";
	let x=0;
	if(showContent[0]==='-'){
		c='-';
		x=1;
		while(x<showContent.length&&!isNaN(showContent[x])){
			c+=showContent[x++];
		}
		arr.push(c);
		c="";
	}
	for(x;x<showContent.length;x++){
		
		if(!isNaN(showContent[x])||showContent[x]==='.'){ //数字
			c+=showContent[x];
		}else{ //符号
			if(c.length!=0){
				arr.push(c);
				c="";
			}
			arr.push(showContent[x]);
		}
		// console.log(arr)
	}
	arr.push(c);
	return arr;
}
function clc(f,a,b){
	if(f==='+') return Number.parseFloat(a)+Number.parseFloat(b);
	if(f==='-') return Number.parseFloat(a)-Number.parseFloat(b);
	if(f==='*') return Number.parseFloat(a)*Number.parseFloat(b);
	if(f==='/') return Number.parseFloat(a)/Number.parseFloat(b);
	if(f==='%') return Number.parseFloat(a)%Number.parseFloat(b);
}
function priorityOpt(opt){ //运算符号的优先级
	if(opt==='+') return 1;
	if(opt==='-') return 1;
	if(opt==='*') return 2;
	if(opt==='/') return 2;
	if(opt==='%') return 3;
}
function getclc(showContent){
	if(isNaN(showContent[showContent.length-1])) return showContent;
	const arr=init(showContent)
	console.log(arr)
	let alpha=[];
	let digit=[];
	for(let x=0;x<arr.length;x++){
		if(isNaN(arr[x])){//符号
			if(alpha.length===0) alpha.push(arr[x]);
			else{
				switch(arr[x]){
					case '%':{ //mod
						while(alpha.length!=0&&!(priorityOpt('%')>priorityOpt(alpha[alpha.length-1]))){
							let b=digit[digit.length-1];
							digit.pop();
							let a=digit[digit.length-1];
							digit.pop();
							digit.push(clc(alpha[alpha.length-1],a,b));
							alpha.pop();
						}
						alpha.push('%');
						break;
					}
					case '+':{
						while(alpha.length!=0&&!(priorityOpt('+')>priorityOpt(alpha[alpha.length-1]))){
							let b=digit[digit.length-1];
							digit.pop();
							let a=digit[digit.length-1];
							digit.pop();
							digit.push(clc(alpha[alpha.length-1],a,b));
							alpha.pop();
						}
						alpha.push('+')
						break;
					}
					case '-':{
						while(alpha.length!=0&&!(priorityOpt('-')>priorityOpt(alpha[alpha.length-1]))){
							let b=digit[digit.length-1];
							digit.pop();
							let a=digit[digit.length-1];
							digit.pop();
							digit.push(clc(alpha[alpha.length-1],a,b));
							alpha.pop();
						}
						alpha.push('-')
						break;
					}
					case '*':{
						while(alpha.length!=0&&!(priorityOpt('*')>priorityOpt(alpha[alpha.length-1]))){
							let b=digit[digit.length-1];
							digit.pop();
							let a=digit[digit.length-1];
							digit.pop();
							digit.push(clc(alpha[alpha.length-1],a,b));
							alpha.pop();
						}
						alpha.push('*')
						break;
					}
					case '/':{
						while(alpha.length!=0&&!(priorityOpt('/')>priorityOpt(alpha[alpha.length-1]))){
							let b=digit[digit.length-1];
							digit.pop();
							let a=digit[digit.length-1];
							digit.pop();
							digit.push(clc(alpha[alpha.length-1],a,b));
							alpha.pop();
						}
						alpha.push('/')
						break;
					}
				}
			}
		}else{
			digit.push(arr[x])
		}
	}
	while(alpha.length!=0){
		let b=digit[digit.length-1];
		digit.pop();
		let a=digit[digit.length-1];
		// console.log(a,alpha[alpha.length-1]," = ",b);
		digit.pop();
		digit.push(clc(alpha[alpha.length-1],a,b));
		alpha.pop();
	}
	return digit[0];
}